<?php
class Gold_Paymentmail_Admin_InfoController extends Mage_Adminhtml_Controller_Action{

   public function indexAction(){
   
    $this->loadLayout()
            ->_setActiveMenu('sales/paymentmail')
            ->_addBreadcrumb(Mage::helper('paymentmail')->__('all payment form'), Mage::helper('paymentmail')->__('all payment form'))
             ->_addContent($this->getLayout()->createBlock('paymentmail/adminhtml_info'))
            ->renderLayout()
            ;
   }
   /**
     * Edit form
     */
    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        $paymentmail = Mage::getModel('paymentmail/paymentmail');
        if ($id){
            $paymentmail->load($id);
        }
        if ($paymentmail->getId() || $id == 0){
            $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
            if (!empty($data)) {
                $paymentmail->setData($data);
            }
            Mage::register('paymentmail_data', $paymentmail);
            $this->loadLayout();
            $this->_setActiveMenu('sales/paymentmail');

            $this->_addBreadcrumb(Mage::helper('paymentmail')->__('payment form'), Mage::helper('paymentmail')->__('payment form'));
            $this->_addBreadcrumb(Mage::helper('paymentmail')->__('payment info edit'), Mage::helper('paymentmail')->__('payment info edit'));
            //$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            $form = $this->getLayout()->createBlock('paymentmail/adminhtml_info_edit');
            $this->_addContent($form);
            $this->renderLayout();
        } else {
            $this->_addError('payment info not exists');
            $this->_redirect('*/*/');
        }
    }
   public function downAction(){
    
      $file = $this->getRequest()->getParam('file');
        try {
            $filePath = Mage::getBaseDir('var').DS.'paymentmail'.DS. $file;
            if ((!is_file($filePath) || !is_readable($filePath)) && !$this->_processDatabaseFile($filePath)) {
                //try get file from quote
                    throw new Exception();
            }
            $this->_prepareDownloadResponse($file, array(
               'value' => $filePath,
               'type'  => 'filename'
            ));
        } catch (Exception $e) {
            $this->_redirect('*/*/');
        } 
   
   }
   /**
     * Check file in database storage if needed and place it on file system
     *
     * @param string $filePath
     * @return bool
     */
    protected function _processDatabaseFile($filePath)
    {
        if (!Mage::helper('core/file_storage_database')->checkDbUsage()) {
            return false;
        }

        $relativePath = Mage::helper('core/file_storage_database')->getMediaRelativePath($filePath);
        $file = Mage::getModel('core/file_storage_database')->loadByFilename($relativePath);

        if (!$file->getId()) {
            return false;
        }

        $directory = dirname($filePath);
        @mkdir($directory, 0777, true);

        $io = new Varien_Io_File();
        $io->cd($directory);

        $io->streamOpen($filePath);
        $io->streamLock(true);
        $io->streamWrite($file->getContent());
        $io->streamUnlock();
        $io->streamClose();

        return true;
    }

}
